package me.liuhui.mall.manager.runtime;

import me.liuhui.mall.manager.service.security.CurrentAdmin;
import me.liuhui.mall.repository.model.Permission;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

/**
 * Created on 2020/10/14 18:04
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */
public class AdminSessionHolder {


    public static CurrentAdmin getCurrentAdmin() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (CurrentAdmin) Optional.ofNullable(authentication).map(Authentication::getPrincipal).orElse(null);
    }

    public static Collection<Permission> getPermission() {
        return Optional.ofNullable(getCurrentAdmin()).map(CurrentAdmin::getPermissions).orElse(Collections.emptyList());
    }



}
